﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Xml;
using Common.Utilities;
using Workflows.Components.Rules.Properties;

namespace Workflows.Components.Rules
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class RuleAction:RuleNodeDataBase
    {
        private FieldSetter2 _Setter;
        /// <summary></summary>
        public FieldSetter2 Setter
        {
            get
            {
                return this._Setter;
            }
            set
            {
                this._Setter = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public RuleAction()
        {
            this.Setter = new FieldSetter2();
            this.Name = "Action";
            this.Description = string.Empty;
            this.Img = Resources.Task2;
            this.RuleNodeType = Rules.RuleNodeType.Action;
        }

        public override System.Xml.XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FieldName",this.Setter.FieldName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FieldDataType", ((int) this.Setter.FieldDataType).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FieldValue",
                                        SimpleDataTypeUtil.ToDbValue(this.Setter.FieldDataType, this.Setter.FieldValue).
                                            ToString());
            return xNode;
        }

        public override RuleNodeDataBase Instantiate(XmlNode xNode)
        {
            RuleAction action=new RuleAction();
            action.RuleNodeID=new Guid(xNode.Attributes.GetNamedItem("RuleNodeID").Value);
            action.RuleNodeType = (RuleNodeType) int.Parse(xNode.Attributes.GetNamedItem("RuleNodeType").Value);
            action.Name = xNode.Attributes.GetNamedItem("Name").Value;
            action.Description = xNode.Attributes.GetNamedItem("Description").Value;
            FieldSetter2 setter=new FieldSetter2();
            setter.FieldName = xNode.Attributes.GetNamedItem("FieldName").Value;
            setter.FieldDataType = (SimpleDataType) int.Parse(xNode.Attributes.GetNamedItem("FieldDataType").Value);
            setter.FieldValue =
                SimpleDataTypeUtil.ToDbValue(setter.FieldDataType, xNode.Attributes.GetNamedItem("FieldValue").Value);
            action.Setter = setter;
            return action;
        }

        public override string DescribeInHtml()
        {
            StringBuilder buffer=new StringBuilder();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(new StringWriter(buffer));
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div);
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.B);
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.U);
            htmlWriter.Write(string.Format("Action {0}:",this.RuleNodeID.ToString()));
            htmlWriter.RenderEndTag();
            htmlWriter.RenderEndTag();
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Br);
            htmlWriter.RenderEndTag();
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Ul);
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Li);
            if (this.Setter != null)
            {
                htmlWriter.Write(
                    string.Format("Set {0} to: {1}", this.Setter.FieldName,
                                  this.Setter.FieldValue == null ? "null" : this.Setter.FieldValue.ToString()));
            }
            else
            {
                htmlWriter.Write("Field setter is not initialized");
            }
            htmlWriter.RenderEndTag();
            htmlWriter.RenderEndTag();
            htmlWriter.RenderEndTag();
            return buffer.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string Describe()
        {
            StringBuilder buffer=new StringBuilder();
            buffer.Append("Action " + this.RuleNodeID.ToString() + Environment.NewLine);
            if(this.Setter !=null)
            {
                FieldSetter2 setter = this.Setter;
                string fieldName = setter.FieldName == null ? "Unknown" : setter.FieldName;
                string fieldValue = setter.FieldValue == null ? "null" : setter.FieldValue.ToString();
                buffer.Append("Set " + fieldName + " to " + fieldValue);
            }
            else
            {
                buffer.Append("Setter is not initialized");
            }
            buffer.Append(Environment.NewLine);
            return buffer.ToString();
        }
    }
}
